Build and Trigger Our First GitHub Action
Learn how to build a GitHub Action by creating a workflow.
We'll cover the following
Now that we have a general understanding of what the components of an action are let's build one and explore how the components are structured and interact.
Creating and cloning a GitHub repository#
If this is our first time creating and cloning a repository, we may find the following links useful:
When creating the repository, we normally add README.md, .gitignore, and an Massachusetts Institute of Technology (MIT) license file. For the sake of this demo, we won't include these in the new repositories we create. Use these commands to clone the repository:
Replace github_repo with the link of your cloned repository and repository_name with the name of your repository. Run it in the terminal below:
While cloning, we may be asked to input our GitHub username and password. For the password, we can add an access token. Details of how you can obtain an access token are provided here. Make sure to select the repo and the workflow scopes while generating this access token.
Creating our first workflow#
Remember that workflows live in the .github/workflows directory. The first step is to create that directory. The next step is to create the workflow file within the .github/workflows directory:
After the cat command above, paste the code below into the terminal. Press “Return” and then “Ctrl+C” after pasting the code.
The preceding workflow is named first-workflow. It will execute a single job called echo on the latest version of Ubuntu and execute a single step that will echo hello world! using the system's default shell. We can also specify the shell we would like with the shell: key.
Commit and push the workflow to GitHub with the following commands:
Run the commands in the terminal above. You will have to fill the commands with your own details, as follows:
github_email: The email of your GitHub account.github_user: The username of your GitHub account.github_repo: The link to your GitHub repository. It should be an empty repository.
In the last command, we'll be asked to input our GitHub username and password for authentication. Use the access token for the password.
Normally, we'd create a branch first and then open a pull request before committing and pushing directly to the main branch, but for our first workflow, this will be the quickest way to see our results.
After we push our commit, we should be able to open our GitHub repository in our browser and click on the Actions tab. We should be greeted with a view of our first workflow having been successfully executed. If not, we can manually run the workflow. It should look like the following:
Note the list of workflows on the left and that there is one workflow named first-workflow. We can see that the first run of the workflow was for our commit with my first action message.
If we click on the workflow run for my first action, we should see the following:
Note the Jobs list on the left with the echo job marked with a green check, signifying the successful execution of the job. On the right, we can see the details of the execution.
We can click on the echo job to see the output from it and the steps that were executed:
Note the job setup, which provides details about the runner and the environment the job executed within. Also, note the echo step single step executed a echo 'Hello World!' single shell command and echoed the "Hello World!" string to the console log. Finally, the job completed successfully due to echo step returning a 0 error code upon completion.
In this lesson, we created our first simple automation. We now have the tools needed to start building more complex automation that will eliminate the toilsome tasks we discussed earlier in the chapter.
Understanding the Basics of GitHub Actions
Building a Continuous Integration Workflow